import Mock from 'mockjs'
import store from '@/store'
import Tools from '@/plugins/tools'

const pageSize = store.state.pageSize
let maxId = 3

const it = {
  identityid: 1,
  teaid: 1,
  identity: '教务处',
  type: 2
}
const ie = {
  identityid: 2,
  teaid: 1,
  identity: '门户维护人员',
  type: 4
}
const iw = {
  identityid: 3,
  teaid: 1,
  identity: '考试中心',
  type: 3
}
const iz = {
  identityid: 4,
  teaid: 1,
  identity: '教师',
  type: 1
}
const extraData = {
  extra: [
    {
      admin_username: 'admin',
      admin_password: 'admin',
      identities: [ie, it, iz, iw],
      admin_status: '1'
    },
    {
      admin_username: 'other',
      admin_password: '123456',
      identities: [it, ie],
      admin_status: '1'
    },
    {
      admin_username: 'test',
      admin_password: '123456',
      identities: [it, ie],
      admin_status: '1'
    },
    {
      admin_username: 'test',
      admin_password: '123456',
      identities: [it, ie],
      admin_status: '1'
    }
  ]
}

let dataListAll = {
  code: 0,
  msg: '操作成功',
  pageSize: pageSize,
  total: maxId
}

let dataListForGroup = {
  code: 0,
  msg: '操作成功'
}

const dataLoginSuccess = {
  code: 200,
  msg: '登录成功',
  data: {

  }
}



const dataLoginError = {
  code: 1,
  msg: '用户名或密码错误'
}

const dataStatusError = {
  code: 1,
  msg: '用户已被禁用，请联系管理员'
}

// 和后端一致，不建议直接返回用户名和密码的判断方式
// Mock.mock(/\/Admin\/check/, 'post', options => {
//   const result = JSON.parse(options.body)
//   let status = '1'
//   if (
//     extraData.extra.some(
//       item =>
//         item.admin_username === result.username &&
//         item.admin_password === result.password &&
//         (dataLoginSuccess.data.identities = item.identities) &&
//         (status = item.admin_status)
//     )
//   ) {
//     return status === '1' ? dataLoginSuccess : dataStatusError
//   }
//   return dataLoginError
// })

Mock.mock(/\/Admin\/listAll/, 'get', options => {
  const p = Tools.getParam('p', options.url)
  const keyword = Tools.getParam('keyword', options.url)
  let tempExtra = extraData.extra // 实际开发中后端会设置返回的字段，不会返回密码等敏感信息
  dataListAll.total = tempExtra.length
  if (keyword !== '' && tempExtra.length > 0) {
    tempExtra = tempExtra.filter(
      item =>
        item.admin_username.toLowerCase().indexOf(keyword.toLowerCase()) !== -1
    )
    dataListAll.total = tempExtra.length
  }
  let pExtraData =
    tempExtra.length > 0
      ? tempExtra.slice(pageSize * (p - 1), pageSize * p)
      : tempExtra
  dataListAll.extra = pExtraData
  return dataListAll
})

Mock.mock(/\/Admin\/listForGroup/, 'get', options => {
  dataListForGroup.extra = extraData.extra.slice(1)
  return dataListForGroup
})

const dataEditSuccess = {
  code: 0,
  msg: '操作成功'
}

const dataEditError = {
  code: 1,
  msg: '用户名不存在或者已删除'
}

const dataExistsError = {
  code: 1,
  msg: '用户名已经存在'
}

const dataSuperError = {
  code: 1,
  msg: '被操作用户是超级用户，操作已取消'
}

const dataSelfError = {
  code: 1,
  msg: '不能对自己执行该操作哦'
}

// Mock.mock(/\/Admin\/add/, 'post', options => {
//   const result = JSON.parse(options.body)
//   const isExists = extraData.extra.some(
//     item =>
//       item.admin_username.toLowerCase() === result.admin_username.toLowerCase()
//   )
//   if (isExists) {
//     return dataExistsError
//   }
//   result.admin_id = ++maxId
//   result.admin_status = '1'
//   extraData.extra.push(result)
//   dataListAll.total++
//   return dataEditSuccess
// })

Mock.mock(/\/Admin\/edit/, 'post', options => {
  // 为了避免前端直接修改localStorage，服务器端会对userid, username和token进行校验，任何一个修改都会校验失败，重新登陆
  const result = JSON.parse(options.body)
  if (result.admin_id === 1 && store.state.userid !== 1) {
    return dataSuperError
  }
  let isExists = false
  extraData.extra = extraData.extra.map(item => {
    if (item.admin_username === result.admin_username) {
      isExists = true
      Object.assign(item, result)
    }
    return item
  })
  if (!isExists) {
    return dataEditError
  }
  return dataEditSuccess
})

Mock.mock(/\/Admin\/detail/, 'get', options => {
  const id = parseInt(Tools.getParam('id', options.url))
  let tempData
  extraData.extra.every(item => {
    if (item.admin_id === id) {
      tempData = {}
      tempData.code = 0
      tempData.msg = '操作成功'
      tempData.extra = item
      return false
    }
    return true
  })
  return tempData || dataEditError
})

Mock.mock(/\/Admin\/changeStatus/, 'post', options => {
  const result = JSON.parse(options.body)
  if (result.admin_id === 1) {
    return dataSuperError
  }
  if (result.admin_id === store.state.userid) {
    return dataSelfError
  }
  let isExists = false
  extraData.extra = extraData.extra.map(item => {
    if (item.admin_id === result.admin_id) {
      isExists = true
      Object.assign(item, result)
    }
    return item
  })
  if (!isExists) {
    return dataEditError
  }
  return dataEditSuccess
})

// Mock.mock(/\/Admin\/del/, 'post', options => {
//   const result = JSON.parse(options.body)
//   const id = result.id
//   if (id === 1) {
//     return dataSuperError
//   }
//   if (id === store.state.userid) {
//     return dataSelfError
//   }
//   let isExists = false
//   extraData.extra = extraData.extra.filter(item => {
//     if (item.admin_id === id) {
//       isExists = true
//     }
//     return item.admin_id !== id
//   })
//   if (!isExists) {
//     return dataEditError
//   }
//   dataListAll.total--
//   dataEditSuccess.total = dataListAll.total
//   return dataEditSuccess
// })

let adminData = [
  {
    teaid: "000001",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000002",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000003",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000004",
    teaname: "王小虎",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐新市区",
    identities: [ie, it],
  },
  {
    teaid: "000005",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [iw],
  },
  {
    teaid: "000006",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000007",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000008",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000009",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000010",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000011",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000012",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000013",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000014",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000015",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000016",
    teaname: "王小虎",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐新市区",
    identities: [ie, it],
  },
  {
    teaid: "000017",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [iw],
  },
  {
    teaid: "000018",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000019",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000020",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000021",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000022",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000023",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000024",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000025",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000026",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000027",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000028",
    teaname: "王小虎",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐新市区",
    identities: [ie, it],
  },
  {
    teaid: "000029",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [iw],
  },
  {
    teaid: "000030",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000031",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000032",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000033",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000034",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000035",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000036",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000037",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000038",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000039",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000040",
    teaname: "王小虎",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐新市区",
    identities: [ie, it],
  },
  {
    teaid: "000041",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [iw],
  },
  {
    teaid: "000042",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000043",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000044",
    teaname: "王志华",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1518 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000045",
    teaname: "郑炅",
    teasex: "女",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "新疆乌鲁木齐天山区",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000046",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000047",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
  {
    teaid: "000048",
    teaname: "刘晓毓",
    teasex: "男",
    passwd: "123456",
    teatel: "13565946671",
    dept: "信息科学与工程学院",
    cashcardid: "26544",
    teaadd: "上海市普陀区金沙江路 1519 弄",
    identities: [ie, it, iz, iw],
  },
];
let a = {
  code: 0,
  msg: "查询成功",
  adminData
}

let teaids = [];
let del = {
  code: 0,
  msg: "删除成功",
  teaids
}
Mock.mock(/\/Admin\/all/, 'post', options => {
  return a;
})

Mock.mock(/\/Admin\/del/, 'post', options => {
  const result = JSON.parse(options.body).teaids;
  console.log(result);
  if (result instanceof Array) {
    for (let i = 0; i < adminData.length; i++) {
      for (let j = 0; j < result.length; j++) {
        if (result[j] === adminData[i].teaid) {
          adminData.splice(i, 1);
        }
      }
    }
  } else {
    adminData = adminData.filter(item => item.teaid !== result)
  }
  return {
    code: 0,
    msg: "删除成功",
    result
  };
})

Mock.mock(/\/Admin\/find/, options => {
  const result = JSON.parse(options.body).teaids;
  let info = {};
  adminData.forEach(item => {
    if(item.teaid === result){
      info = item;
    }
  })
  return {
    code:0,
    msg:"查询成功",
    data:info
  }
})

Mock.mock(/\/Admin\/delete/, options => {
  // console.log(options.url)
  const id = Tools.getParameter('id', options.url)
  // adminData.filter(data => data.teaid != id));
  // console.log(adminData);
  return del;
})
let updateSuccess = {
  code:0,
  msg:"修改成功"
}
Mock.mock(/\/Admin\/update/, options => {
  const result = JSON.parse(options.body).adminInfo;
  let teaid = result.teaid;
  // let teaname = result.teaname;
  // let teasex = result.teasex;
  // let teatel = result.teatel;
  // let passwd = result.passwd;
  // let dept = result.dept;
  // let cashcardid = result.cashcardid;
  // let teaadd = result.teaadd;
  let data = {};
  adminData.forEach(item => {
    if(item.teaid === teaid){
      // item.teaname = teaname;
      // item.teasex = teasex;
      // item.teatel = teatel;
      // item.passwd = passwd;
      // item.dept = dept;
      // item.cashcardid = cashcardid;
      // item.teaadd = teaadd;
      item = result;
      data = item;
    }
  })
  return {
    code:0,
    msg:"修改成功",
    item:result
  }
});


Mock.mock(/\/Admin\/add/,options => {
  const result = JSON.parse(options.body).adminInfo;
  adminData.push(result);
  return {
    code:0,
    msg:"添加成功"
  }
})

Mock.mock(/\/Admin\/check/, 'post', options => {
  const result = JSON.parse(options.body);
  console.log(result)

  // console.log(adminData.some(
  //   item =>
  //     item.teaid === result.teaid &&
  //     item.passwd === result.passwd &&
  //     (dataLoginSuccess.data.identities = item.identities) 
  // ))
  if(
    adminData.some(
      item =>
        item.teaid === result.teaid &&
        item.passwd === result.passwd &&
        (dataLoginSuccess.data.identities = item.identities)
    )
  ) {
    return dataLoginSuccess 
  }
  return dataLoginError
})
